<template>
	<view class="outBox" @click.stop="closeInfoBox">
		<u-navbar leftText="我的社区" leftIconColor="#fff" :autoBack="true" bgColor="rgba(0, 0, 0, 0.6)">
			<view class="left-slot" slot="left">
				<u-icon name="arrow-left" color="#fff"></u-icon>
				<text>{{ '我的社区' }}</text>
			</view>
		</u-navbar>

		<VideoBox>
			<template #content>
				<view class="content content2" v-if="infoDetails.saleTeamStatus == 1">
					<view class="btnBox">
						<view class="showInfoBox showInfoBox3">
							<view class="tit">
								<text>社区说明：</text>
							</view>
							<view class="text2">
								<text style="padding-right: 10rpx;">*</text>
								<text>拥有8名及以上弟弟活跃账户（</text>
								<text style="color: #0FC6C2">4≤</text>
								<text>本月订台数量；</text>
								<text style="color: #0FC6C2">4000≤</text>
								<text>本月消费金额）解锁主理人绩效，如本月未满足弟弟活跃账户条件，主理人绩效取消。</text>
							</view>
							<view class="text">
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">20万≤本月社区业绩≤40万</text>
									<text>，绩效2.8%。</text>
								</view>
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">40万≤本月社区业绩≤60万</text>
									<text>，绩效2.9%。</text>
								</view>
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">60万≤本月社区业绩≤60万</text>
									<text>，绩效3%。</text>
								</view>
							</view>
						</view>
						<view class="btns">
							<view class="btnOut btnOut4">
								<view class="btn btn4" @click="handleCreate">
									成立社区
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="content content2" v-else-if="infoDetails.saleTeamStatus == 2">
					<view class="btnBox">
						<view class="showInfoBox showInfoBox3">
							<view class="tit">
								<text>社区说明：</text>
							</view>
							<view class="text2">
								<text style="padding-right: 10rpx;">*</text>
								<text>拥有8名及以上弟弟活跃账户（</text>
								<text style="color: #0FC6C2">4≤</text>
								<text>本月订台数量；</text>
								<text style="color: #0FC6C2">4000≤</text>
								<text>本月消费金额）解锁主理人绩效，如本月未满足弟弟活跃账户条件，主理人绩效取消。</text>
							</view>
							<view class="text">
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">20万≤本月社区业绩≤40万</text>
									<text>，绩效2.8%。</text>
								</view>
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">40万≤本月社区业绩≤60万</text>
									<text>，绩效2.9%。</text>
								</view>
								<view class="one">
									<text style="padding-right: 10rpx;">*</text>
									<text style="color: #0FC6C2">60万≤本月社区业绩≤60万</text>
									<text>，绩效3%。</text>
								</view>
							</view>
						</view>
						<view class="btnJs">
							<text>审核中</text>
							<image :src="getImgUrl(`reservation/img3.png`)" mode="widthFix" class="iconImg"></image>
						</view>
					</view>

				</view>
				<view class="content" v-else>
					<view class="selectTime">
						<ep-select v-model="selectTime" :options="monthList" @change="changeTime"></ep-select>
					</view>
					<view class="bindingBox">
						<view class="user" @click="handleUpdate">
							<view class="img">
								<image :src="infoDetails.avatar" mode="scaleToFill"></image>
							</view>
							<view class="showInfoBox showInfoBox1" :style="{
              background:`url(${baseImgUrl}manager/bg2.png)`,
              backgroundSize:'100% 100%',
          display: showInfoBox1 ? 'block' : 'none'
            }">
								<view class="tit">
									<text>社区说明：</text>
								</view>
								<view class="text2">
									<text style="padding-right: 10rpx;">*</text>
									<text>拥有8名及以上弟弟活跃账户（</text>
									<text style="color: #0FC6C2">4≤</text>
									<text>本月订台数量；</text>
									<text style="color: #0FC6C2">2000≤</text>
									<text>本月业绩）解锁主理人绩效。</text>
								</view>
								<view class="text">
									<view class="one">
										<text style="padding-right: 10rpx;">社区团队业绩：</text>
										<text style="color: #0FC6C2">200000-499999.99元</text>
										<text>，比例为2.6%。</text>
									</view>
									<view class="one">
										<text style="padding-right: 10rpx;">社区团队业绩：</text>
										<text style="color: #0FC6C2">500000-999999.99元</text>
										<text>，比例为2.8%。</text>
									</view>
									<view class="one">
										<text style="padding-right: 10rpx;">社区团队业绩：</text>
										<text style="color: #0FC6C2">1000000元及以上</text>
										<text>，比例为3%。</text>
									</view>
								</view>
								<view class="tit">
									<text>主理人分佣：</text>
								</view>
								<view class="text">
									<view class="one">
										<text style="padding-right: 10rpx;">本账户为活跃账户（</text>
										<text style="color: #0FC6C2">4≤本月定台数量，或2000≤本月业绩</text>
										<text>）解锁主理人分佣。</text>
									</view>
									<view class="one">
										<text>主理人账户业绩的5%，作为份拥，由平台直接发放至主理人账户。</text>
									</view>
								</view>
							</view>
							<view class="dis-flex">
								<view class="name">
									{{ infoDetails.teamName || '' }}
								</view>
								<image class="iconImg" @click.stop="handleShowBox1"
									:src="`/static/reservation/img2.png`" mode="widthFix"></image>
							</view>
						</view>
					</view>
					<view class="items1">
						<view class="item1">
							<view class="label">
								<text>成立时间</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.createTime }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>社区人数</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.teamNum || 0 }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>业绩总金额</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.performance || 0 }}元</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>发起拼卡门票总金额</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.ticketPrice || 0 }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>发起拼卡总数量</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.ticketNum || 0 }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>组局总数量</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.setsNum || 0 }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>VIP组局总数量</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.setsVipNum || 0 }}</text>
							</view>
						</view>
						<view class="item1">
							<view class="label">
								<text>邀约总数量</text>
							</view>
							<view class="value">
								<text>{{ infoDetails.invitationNum || 0 }}</text>
							</view>
						</view>


					</view>
					<view class="bindList">
						<view class="title">
							<text>社区成员</text>
						</view>

						<view class="items">
							<view class="item" :style="{
                        backgroundImage: `url(${itemBg})`,
                        backgroundSize: '100% 100%'
                      }" v-for="item in infoDetails.teamMemberList">
								<image :src="item.avatar" mode="scaleToFill"></image>
								<text class="name">{{ item.memberName }}
									{{ item.isTeamLeader == 1 ? '(主理人)' : '' }}</text>
								<text class="phone">{{ item.memberMobile }}</text>
							</view>
						</view>
					</view>
					<view class="btnJs" v-if="infoDetails.myselfIsTeamLeader == 1" @click="handleUnBanding">
						<text>解散社区</text>
						<image :src="getImgUrl(`reservation/img3.png`)" mode="widthFix" class="iconImg"></image>
					</view>
					<view class="btns btns2" v-else>
						<view class="btnOut btnOut4">
							<view class="btn btn4" @click="this.$refs.diaBoxRef3.show = true">
								成立社区
							</view>
						</view>
					</view>
				</view>
			</template>
		</VideoBox>
		<diaBox ref="diaBoxRef3" title="创建社区" content="如果申请建立社区您将退出当前的社区，是否申请建立？ " :show-btns="true"
			@comfireFunc="handleCreate" />
		<diaBox ref="diaBoxRef2" title="解散社区" content="是否确认解散？ " :show-btns="true" @comfireFunc="unBindFunc" />
		<diaBox ref="diaBoxRef" :title="diaTitle" :show-btns="false">
			<template v-slot:dia-cont>
				<view class="diaCont">
					<view class="uploadBox">
						<view class="tit">上传头像</view>
						<view class="img">
							<button size="default" class="avatar-wrapper" open-type="chooseAvatar"
								@chooseavatar="onChooseAvatar">
								<image v-if="!fakeImgUrl" :src="`${baseImgUrl}manager/img1.png`" mode="widthFix" />
								<image v-else :src="fakeImgUrl" mode="scaleToFill" />
							</button>
						</view>

					</view>
					<view class="inputBox">

						<u--input placeholder="请输入社区名称" border="surround" v-model="name" color="#fff" :placeholderStyle="{
                  color:'#707a84'
                }"></u--input>
					</view>

					<view class="btns">
						<view class="btnOut">
							<view class="btn" @click="handleSubmit">
								提交
							</view>
						</view>
					</view>
				</view>
			</template>
		</diaBox>
	</view>
</template>

<script>
	import {
		getWXStatusHeight,
		getImgUrl
	} from '@/utils/index.js'

	import {
		disbandSaleTeam,
		findSaleTeamDetail,
		initiateSaleTeamApi,
		modifySaleTeamInfo,
		saleSystemInfo,
		monthListApi
	} from '../../api/api'

	import diaBox from "@/components/diaBox/index.vue"

	import itemBg from "@/static/order/itemBg.png"
	import VideoBox from "@/components/VideoBox/index.vue";

	export default {
		components: {
			VideoBox,
			diaBox
		},
		data() {
			return {
				showInfoBox1: false,
				baseImgUrl: this.$baseImgUrl,
				// 胶囊的宽度
				barWidth: 0,
				menuHeight: 0,
				selectPeople: '1',
				videoUrl: this.$videoUrl,
				// 社区名称
				name: '',
				partAvatar: '',
				fakeImgUrl: '',
				diaTitle: '成立社区',
				options: [],
				// 显示规则
				showRule: false,
				transference: false,
				// 分销体系详情
				infoDetails: {},
				itemBg: itemBg,
				selectTime: "",
				monthList: [],
			}
		},
		onLoad() {
			let obj = getWXStatusHeight()
			this.barWidth = obj.barWidth + 3
			this.menuHeight = obj.menuHeight

			this.getDetailsInfo()
			// 生成最近6个月的月份列表
			// this.monthList = this.getRecentMonths().map(item => {
			// 	return {
			// 		label: item,
			// 		value: item
			// 	}
			// })
			this.getMonthList()
		},
		methods: {
			getImgUrl,
			async getMonthList() {
				const res = await monthListApi()
				if (res.code == 200) {
					this.monthList = res.data.map(item => {
						return {
							value: item,
							label: item
						}
					})
				}
			},
			getRecentMonths(count = 6) {
				const months = [];
				const today = new Date();
				let year = today.getFullYear();
				let month = today.getMonth();

				for (let i = 0; i < count; i++) {
					let yearStr = year.toString();
					let monthStr = (month + 1).toString().padStart(2, '0'); // 月份从0开始计数，所以加1
					months.push(`${yearStr}-${monthStr}`);

					// 减少月份，如果月份小于0，则减少年份并重置月份为11（即12月）
					if (month > 0) {
						month--;
					} else {
						year--;
						month = 11;
					}
				}

				return months; // 最新的月份排在最后，所以反转数组
			},
			// 提交
			handleSubmit() {
				if (this.diaTitle == '成立社区') {
					this.handleCreatePart()
				} else {
					this.updatePartInfo()
				}
			},
			// 创建社区
			handleCreate() {
				this.$refs.diaBoxRef.show = true
				this.$refs.diaBoxRef3.close()
				this.diaTitle = '成立社区'
			},
			// 更新社区
			async updatePartInfo() {
				if (!this.partAvatar) {
					this.$u.toast('请上传社区头像')
					return
				}

				if (!this.name) {
					this.$u.toast('请输入社区名称')
					return
				}

				const data = {
					"saleTeamId": this.infoDetails.saleTeamId,
					"teamName": this.name,
					"avatar": this.partAvatar
				}
				const res = await modifySaleTeamInfo(data)
				if (res.code == 200) {
					this.$u.toast('修改成功')
					this.$refs.diaBoxRef.close()
					await this.getDetailsInfo()
				} else {
					this.$u.toast(res.msg)
				}
			},
			// 点击更新社区
			handleUpdate() {
				if (this.infoDetails.myselfIsTeamLeader != 1) return
				this.$refs.diaBoxRef.show = true
				this.name = this.infoDetails.teamName
				this.partAvatar = this.infoDetails.avatar.replace(/^(https?:\/\/[^\/]+)\//, '/');
				this.fakeImgUrl = this.infoDetails.avatar
				this.diaTitle = '更新社区'
			},
			// 解散社区
			async unBindFunc() {
				const res = await disbandSaleTeam(this.infoDetails.saleTeamId)
				if (res.code == 200) {
					uni.$u.toast(res.msg);
					await this.getDetailsInfo()
					this.$refs.diaBoxRef2.close()
				}
			},
			// 取消绑定
			handleUnBanding() {
				this.$refs.diaBoxRef2.show = true
			},
			closeInfoBox() {
				this.showInfoBox1 = false
			},
			handleShowBox1() {
				this.showInfoBox1 = true
			},
			// 设置头像
			async onChooseAvatar(e) {
				const data = {
					file: e.detail.avatarUrl
				}

				const token = uni.getStorageSync("token");

				let a = uni.uploadFile({
					url: this.$baseUrl + "common/upload",
					filePath: e.detail.avatarUrl,
					name: 'file',
					header: {
						Authorization: "Bearer " + token
					},
					success: (res) => {
						const res1 = JSON.parse(res.data)
						this.partAvatar = res1.fileName
						this.fakeImgUrl = res1.url
					}
				})
			},
			// 成立社区
			async handleCreatePart() {
				if (!this.partAvatar) {
					this.$u.toast('请上传社区头像')
					return
				}

				if (!this.name) {
					this.$u.toast('请输入社区名称')
					return
				}

				const data = {
					"teamName": this.name,
					"avatar": this.partAvatar
				}
				const res = await initiateSaleTeamApi(data)
				if (res.code == 200) {
					this.$u.toast('创建成功')
					this.$refs.diaBoxRef.close()
					this.getDetailsInfo()
				} else {
					this.$u.toast(res.msg)
				}
			},
			// 获取详情
			async getDetailsInfo() {
				const res = await findSaleTeamDetail(this.selectTime)
				this.infoDetails = res.data
			},
			// 转让
			handleTransference() {
				this.transference = false
			},
			changeTime(e) {
				this.selectTime = e
				this.getDetailsInfo()
			},
			handleShowRule() {
				this.showRule = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.selectTime {
		width: 200rpx;
	}

	.btns2 {
		margin-bottom: 50rpx;

		.btnOut {
			width: 100% !important;

			.btn {
				width: 100% !important;
			}
		}
	}

	.btnJs {

		position: relative;

		text {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			font-weight: 600;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 36rpx;
			text-align: left;
			font-style: normal;
		}

		image {
			width: 100%;
		}
	}

	.dis-flex {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 32rpx;

		image {
			width: 36rpx !important;
			margin-left: 10rpx;
		}
	}

	.showInfoBox {
		width: 582rpx;
		box-sizing: border-box;
		padding: 68rpx 50rpx 45rpx;
		position: absolute;
		z-index: 5;
		top: 92%;
		left: 66rpx;
		font-size: 28rpx !important;
		font-weight: 400 !important;

		.tit {
			margin-bottom: 30rpx;
			color: #0FC6C2;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
		}

		.one {
			margin-bottom: 20rpx;
		}

		.text2 {
			font-weight: 400;
			color: #FFFFFF;
			line-height: 44rpx;
			text-align: justify;
			font-style: normal;
			margin-bottom: 32rpx;
		}

		.text {
			font-weight: 400;
			color: #FFFFFF;
			line-height: 44rpx;
			text-align: justify;
			font-style: normal;
			margin-bottom: 32rpx;
			display: flex;
			flex-direction: column;

			&:last-child {
				margin-bottom: 0;
			}
		}

		image {
			width: 32rpx;
		}
	}


	.diaCont {
		.uploadBox {
			margin-top: 52rpx;

			.tit {
				text-align: center;
				font-weight: 600;
				font-size: 28rpx;
				color: #FFFFFF;
				line-height: 32rpx;
				text-align: center;
				font-style: normal;
				margin-bottom: 38rpx;
			}

			.img {
				text-align: center;

				image {
					width: 196rpx;
				}
			}
		}

		.inputBox {
			width: 552rpx;
			height: 112rpx;
			background: rgba(15, 15, 15, 0.41);
			border-radius: 40rpx;
			border: 2rpx solid #35383b;
			backdrop-filter: blur(7px);
			padiding: 0 32rpx;
			line-height: 112rpx;
			display: flex;
			align-items: center;
			margin-top: 30rpx;
		}

		.btns {
			.btn {
				width: 100% !important;
			}
		}
	}

	::v-deep .avatar-wrapper {
		background: transparent;
	}

	.avatar-wrapper {
		background: transparent;
		width: 196rpx;
		height: 196rpx;
		border-radius: 50%;
		overflow: hidden;
		padding: 0 !important;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}
	}

	.btns {
		margin-top: 46rpx;

		.btnOut {
			border-radius: 64rpx;
			box-shadow: 0rpx 0rpx 40rpx 38rpx rgba(15, 198, 194, 0.25);
		}

		.btn {
			font-size: 32rpx;
			text-align: center;
			color: #fff;
			width: 400rpx;
			height: 110rpx;
			line-height: 110rpx;
			background: #0FC6C2;
			border-radius: 64rpx;
			box-shadow: inset 0rpx 14rpx 34rpx 0rpx #FFFFFF;
			position: relative;

			.iconImg {
				width: 32rpx;
				margin-left: 8rpx;
			}

			.myText {
				text {
					font-weight: 600;
					font-size: 36rpx;
					color: #FFFFFF;
					line-height: 36rpx;
					text-align: left;
					font-style: normal;
				}
			}
		}

		.btn4 {
			background: #f97609;
		}

		.btnOut4 {
			width: 400rpx;
			border-radius: 64rpx;
			box-shadow: 0rpx 0rpx 40rpx 38rpx rgba(249, 118, 9, 0.25);
		}

	}

	.content2 {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.btnBox {
		margin-top: 100rpx;

		.btns {
			display: flex;
			justify-content: center;
			margin-top: 60rpx;
		}

		.showInfoBox3 {
			position: relative !important;
			top: auto !important;
			left: auto !important;
			background: #000;
			border-radius: 60rpx;
		}
	}

	.items1 {
		background: rgba(0, 0, 0, 0.52);
		border-radius: 60rpx;
		border: 2rpx solid;
		padding: 48rpx;
		border: 2rpx solid #21253B;
		margin-top: 30rpx;
		backdrop-filter: blur(7px);

		.item1 {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 112rpx;

			.label {
				font-weight: 500;
				font-size: 28rpx;
				color: #86909C;
				line-height: 28rpx;
				text-align: left;
				font-style: normal;
			}

			.value {
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
				line-height: 28rpx;
				text-align: right;
				font-style: normal;
			}
		}
	}

	.bindList {
		background: rgba(0, 0, 0, 0.52);
		border-radius: 60rpx;
		border: 2rpx solid;
		padding: 48rpx;
		border: 2rpx solid #21253B;
		margin-top: 30rpx;
		backdrop-filter: blur(7px);

		.items {

			.item {
				margin-top: 30rpx;
				width: 588rpx;
				height: 140rpx;
				background-size: 100% 100%;
				display: flex;
				align-items: center;

				image {
					width: 84rpx;
					height: 84rpx;
					border-radius: 50%;
					margin-right: 20rpx;
					margin-left: 30rpx;
				}

				.name {
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 28rpx;
					text-align: left;
					font-style: normal;
					width: 196rpx;
				}

				.phone {
					font-weight: 500;
					font-size: 28rpx;
					color: #86909C;
					line-height: 28rpx;
					text-align: left;
					font-style: normal;
				}

				text {
					margin-left: 30rpx;
				}
			}
		}

		.title {
			font-weight: 400;
			height: 60rpx;
			font-size: 28rpx;
			color: #86909C;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
			padding-bottom: 20rpx;
			display: flex;
			align-items: center;
			position: relative;

			image {
				width: 32rpx;
				margin-left: 16rpx;
			}
		}
	}

	.bindingBox {
		border-radius: 40rpx;
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: relative;

		.user {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.img {
				width: 240rpx;
				height: 240rpx;
				border-radius: 106rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.name {
				text-align: center;
				font-weight: 600;
				font-size: 40rpx;
				color: #FFFFFF;
				line-height: 40rpx;
				text-align: center;
				font-style: normal;
			}

			image {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

	}

	.bgBox {
		width: 100vw;
		height: 100vh;
		position: fixed;
		z-index: 1;
		background: rgba(0, 0, 0, 0.6);
	}

	video {
		width: 100%;
		height: 100%;
		object-fit: cover;
		/* 确保视频填充整个容器，可能会裁剪视频 */
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 0;
	}

	.content {
		position: relative;
		margin: 40rpx;
		box-sizing: border-box;
		z-index: 9;
		flex: 1;
	}

	.left-slot {
		color: #fff;
		display: flex;
		align-items: center;

		image {
			width: 40rpx;
			margin-right: 30rpx;
		}
	}

	.outBox {
		height: 100vh;
		display: flex;
		flex-direction: column;
	}
</style>